package com.yydm.serviceorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPayUtil;
import com.yydm.serviceorder.entity.AutomobileOrder;
import com.yydm.serviceorder.entity.AutomobileOrderLog;
import com.yydm.serviceorder.mapper.AutomobileOrderLogMapper;
import com.yydm.serviceorder.service.IAutomobileOrderLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yydm.serviceorder.service.IAutomobileOrderService;
import com.yydm.serviceorder.utils.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yydm
 * @since 2022-07-14
 */
@Service
public class AutomobileOrderLogServiceImpl extends ServiceImpl<AutomobileOrderLogMapper, AutomobileOrderLog> implements IAutomobileOrderLogService {

    @Autowired
    private IAutomobileOrderService automobileOrderService;


    @Override
    public Map<String, String> queryStatus(String orderId) {
        try {
//1、封装参数
            Map m = new HashMap<>();
            m.put("appid", "wx74862e0dfcf69954");
            m.put("mch_id", "1558950191");
            m.put("out_trade_no", orderId);
            m.put("nonce_str", WXPayUtil.generateNonceStr());
//2、设置请求
            HttpClient client = new
                    HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            client.setXmlParam(WXPayUtil.generateSignedXml(m,
                    "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb"));
            client.setHttps(true);
            client.post();
//3、返回第三方的数据
            String xml = client.getContent();
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
//6、转成Map
//7、返回
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateOrderStatus(Map<String, String> map) {
//          从map获取订单号
        String orderId = map.get("out_trade_no");
//        根据订单号查询订单信息
        QueryWrapper<AutomobileOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        AutomobileOrder order = automobileOrderService.getOne(wrapper);

//        更新订单表订单状态
        if (order.getOrderStatus().intValue() == 1) {
            return;
        }
        order.setOrderStatus(1);  //代表已支付
        automobileOrderService.updateById(order);

//        向支付表添加支付记录
        AutomobileOrderLog automobileOrderLog = new AutomobileOrderLog();

        automobileOrderLog.setOrderId(orderId);
        automobileOrderLog.setCompleteTime(new Date());
        automobileOrderLog.setOrderType(1);
        automobileOrderLog.setOrderPrice(order.getOrderPrice());

        automobileOrderLog.setOrderStatus(map.get("order_status"));
        automobileOrderLog.setTransactionId(map.get("transaction_id"));

        baseMapper.insert(automobileOrderLog);
    }
}
